package com.hspedu.lambda;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * Java内置的四大函数式接口
 *
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/29
 * @time 23:08
 */
public class LambdaTest2 {
    @Test
    public void test1() {
        happyTime(500, money -> System.out.println("消费了" + money));
    }

    public void happyTime(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

    @Test
    public void test2(){
        List<String> list = Arrays.asList("北京", "河南", "南京", "普京", "东京");
        List<String> filter = strFilter(list, s -> s.contains("京"));
        for (String s : filter) {
            System.out.print(s +" ");
        }
    }

    /**
     * 根据给定的规则，过滤集合中的字符串
     * 这个规则是由程序员调用方法时自己写的接口实现类
     * 如何实现方法决定的
     * @param list
     * @param predicate
     * @return
     */
    public List<String> strFilter(List<String> list , Predicate<String> predicate){
        ArrayList<String> strings = new ArrayList<>();
        for (String s : list) {
            if (predicate.test(s)){
                strings.add(s);
            }
        }
        return strings;
    }
}
